package com.example.netchat;

import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class RoomListFragment extends ListFragment implements
		LoaderManager.LoaderCallbacks<Cursor> {

	public static final String ARGUMENT_ONLY_FAVORITES = "ONLY_FAVORITES";

	// This is the Adapter being used to display the list's data.
	SimpleCursorAdapter mAdapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = super.onCreateView(inflater, container, savedInstanceState);

		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		// Give some text to display if there is no data. In a real
		// application this would come from a resource.
		setEmptyText("No favorites");

		// We have a menu item to show in action bar.
		setHasOptionsMenu(true);

		// Create an empty adapter we will use to display the loaded data.
		mAdapter = new RoomListAdapter(getActivity(), null,
				(RoomListActionListener) getActivity());

		setListAdapter(mAdapter);

		// Start out with a progress indicator.
		setListShown(false);

		// Prepare the loader. Either re-connect with an existing one,
		// or start a new one.
		getLoaderManager().initLoader(0, null, this);
	}

	public Loader<Cursor> onCreateLoader(int id, Bundle args) {

		Uri baseUri = RoomsContentProvider.CONTENT_URI;

		String selection;

		Bundle arguments = getArguments();
		if (arguments != null && arguments.containsKey(ARGUMENT_ONLY_FAVORITES)
				&& arguments.getBoolean(ARGUMENT_ONLY_FAVORITES)) {
			selection = RoomsContentProvider.ALL_COLUMNS[RoomsContentProvider.FAVORITE_COLUMN_INDEX]
					+ "=1";

		} else {
			selection = null;
		}

		return new CursorLoader(getActivity(), baseUri,
				RoomsContentProvider.ALL_COLUMNS, selection, null,
				RoomsContentProvider.ROOM_NAME_COLUMN_INDEX
						+ " COLLATE LOCALIZED ASC");
	}

	public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
		// Swap the new cursor in. (The framework will take care of closing the
		// old cursor once we return.)

		if (data != null) {
			data.setNotificationUri(getActivity().getContentResolver(),
					RoomsContentProvider.CONTENT_URI);
		}

		mAdapter.swapCursor(data);

		// The list should now be shown.
		if (isResumed()) {
			setListShown(true);
		} else {
			setListShownNoAnimation(true);
		}
	}

	public void onLoaderReset(Loader<Cursor> loader) {
		// This is called when the last Cursor provided to onLoadFinished()
		// above is about to be closed. We need to make sure we are no
		// longer using it.
		mAdapter.swapCursor(null);
	}

}